package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduMajorDao;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.edu.rep.EduMajorRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class EduMajorDaoImpl extends MysBaseDao implements EduMajorDao {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorDaoImpl.class);

	@Override
	public Integer create(final EduMajor arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO edu_major (major, module, ness, detail) VALUES"
				+ " (:major, :module, ness, :detail)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduMajor arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Update 】- 开始运行 ");

		final String query = "UPDATE edu_major SET major = :major, ness = :ness, detail = :detail WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 Delete 】- 开始运行 ");

		final String query = "DELETE FROM edu_major WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM edu_major " + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public EduMajor queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM edu_major WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduMajor entity = queryObject(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public EduMajor queryByBunchId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaBunchDao 】-【 QueryMajorById 】- 开始运行 ");

		final String query = "SELECT a.id, a.module FROM edu_major AS a, lea_term AS b, lea_bunch AS c"
				+ " WHERE a.id = b.major_id AND b.id = c.term_id AND c.id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduMajor entity = queryObject(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】-【 LeaBunchDao 】-【 QueryMajorById 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<EduMajor> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_major";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduMajor> array = queryList(query, EduMajor.class);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduMajor> queryList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_major" + map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduMajor> array = queryList(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduMajor> queryListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryListById 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_major AS a, edu_project_major AS b"
				+ " WHERE a.id = b.major_id AND b.project_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduMajor> array = queryList(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryListById 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduMajorRep> queryRepListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryRepListById 】- 开始运行 ");

		final String query = "SELECT a.id, a.major, a.module, b.id AS viewId FROM edu_major AS a, edu_project_major AS b"
				+ " WHERE a.id = b.major_id AND b.project_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduMajorRep> array = queryList(query, EduMajorRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryRepListById 】- 完成运行 ");
		return array;
	}

}
